/**
 *   _                                                          _   
 *  | | ____ _ _ __ _ __ ___   __ _  ___           _ __ ___ ___| |_ 
 *  | |/ / _` | '__| '_ ` _ \ / _` |/ __|  _____  | '__/ _ | __| __|
 *  |   < (_| | |  | | | | | | (_| | (__  |_____| | | |  __|__ \ |_ 
 *  |_|\_\__,_|_|  |_| |_| |_|\__,_|\___|         |_|  \___|___/\__|
 *         
 *  Copyright  2011 Kondra, Inc. 
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.kondra.karmac.rest
{
	import flash.net.URLVariables;

   [DefaultProperty( "services" )]

   /**
    * The <code>BatchServiceEndpoint</code> class represents a group of 
    * <code>ServiceRef</code> objects that can be executed in parallel
    * or sequentially.
    * 
    * @author Jared Kaplan
    */  
   public class BatchServiceEndpoint extends AbstractServiceEndpoint
   {
      private var m_serviceList:Array;
      private var m_sequence:Boolean;

      public function BatchServiceEndpoint() {
         m_serviceList = new Array();  
      }
      
      /**
       * Adds a <code>ServiceRef</code> object to this batch endpoint.
       * 
       * @param ref          The service reference object being added to the batch request
       */ 
      public function add( ref:ServiceRef ) : void {
         m_serviceList.push( ref );
      }
      
      /**
       * @inheritDoc 
       */  
      public override function createServiceTask( data:Object = null, completeHandler:Function = null, errorHandler:Function = null, progressHandler:Function = null ) : IServiceTask 
      {
         var task:BatchServiceTask = new BatchServiceTask( this );
         var service:IServiceEndpoint = null;
         
         for each( var next:ServiceRef in m_serviceList )
         {
            service = next.getService();
            
            if( service != null )
            {
			   var serviceData:Object = createParams( next.data != null ? createUrlVariables( next.data ) : null );

               // add service task to batch task list
               task.add( service.createServiceTask( serviceData ), next.key );
            }
         }
         
         task.sequence = sequence;
         task.addNotificationHandler( new ServiceNotificationHandler( completeHandler, errorHandler, progressHandler ) );
         
         return task;
      }
      
      //
      // getter/setter methods
      //
      [ArrayElementType("com.kondra.karmac.rest.ServiceRef")]
      public function set services( services:Array ) : void
      {
         for each( var next:ServiceRef in services )
            m_serviceList.push( next );      
      }
      
      public function set sequence( sequence:Boolean ) : void { m_sequence = sequence; }
      public function get sequence() : Boolean { return m_sequence; }
   }
}